package com.lineage.william;

import com.lineage.DatabaseFactory;
import com.lineage.server.datatables.ItemTable;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Clan;
import com.lineage.server.model.L1Teleport;
import com.lineage.server.model.skill.L1SkillUse;
import com.lineage.server.serverpackets.S_DoActionGFX;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1Item;
import com.lineage.server.timecontroller.server.ServerUseMapTimer;
import com.lineage.server.world.WorldClan;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.StringTokenizer;

public class castleid_npc {
  private static ArrayList<ArrayList<Object>> aData = new ArrayList<>();
  
  private static boolean NO_GET_DATA = false;
  
  public static final String TOKEN = ",";
  
  public static boolean forNpcQuest(String s, L1PcInstance pc, L1NpcInstance npc, int npcid, int oid) {
    ArrayList<Object> aTempData = null;
    if (!NO_GET_DATA) {
      NO_GET_DATA = true;
      getData();
    } 
    int i = 0;
    while (i < aData.size()) {
      aTempData = (ArrayList)aData.get(i);
      if (aTempData.get(0) != null && ((Integer)aTempData.get(0)).intValue() == npcid && (
        (String)aTempData.get(1)).equals(s)) {
        boolean hascastle = checkHasCastle(pc, ((Integer)aTempData.get(15)).intValue());
        if (hascastle) {
          if (((Integer)aTempData.get(2)).intValue() != 0 && 
            pc.getLevel() < ((Integer)aTempData.get(2)).intValue()) {
            pc.sendPackets(
                (ServerBasePacket)new S_SystemMessage("等级最低需求" + ((Integer)aTempData.get(2)).intValue() + "才可使用"));
            return false;
          } 
          if (((Integer)aTempData.get(3)).intValue() != 0) {
            byte class_id = 0;
            String msg = "";
            if (pc.isCrown()) {
              class_id = 1;
            } else if (pc.isKnight()) {
              class_id = 2;
            } else if (pc.isWizard()) {
              class_id = 3;
            } else if (pc.isElf()) {
              class_id = 4;
            } else if (pc.isDarkelf()) {
              class_id = 5;
            } else if (pc.isDragonKnight()) {
              class_id = 6;
            } else if (pc.isIllusionist()) {
              class_id = 7;
            } 
            switch (((Integer)aTempData.get(3)).intValue()) {
              case 1:
                msg = "王族";
                break;
              case 2:
                msg = "骑士";
                break;
              case 3:
                msg = "法师";
                break;
              case 4:
                msg = "妖精";
                break;
              case 5:
                msg = "黑暗妖精";
                break;
              case 6:
                msg = "龙骑士";
                break;
              case 7:
                msg = "幻术士";
                break;
            } 
            if (((Integer)aTempData.get(3)).intValue() != class_id) {
              pc.sendPackets((ServerBasePacket)new S_SystemMessage("你的职业无法使用" + msg + "的专属道具。"));
              return false;
            } 
          } 
          Calendar date = Calendar.getInstance();
          int nowWeek = date.get(7) - 1;
          int nowHour = date.get(11);
          if (nowWeek == 0)
            nowWeek += 7; 
          String week = null;
          if (((Integer)aTempData.get(12)).intValue() == 1) {
            week = "ㄧ";
          } else if (((Integer)aTempData.get(12)).intValue() == 2) {
            week = "二";
          } else if (((Integer)aTempData.get(12)).intValue() == 3) {
            week = "三";
          } else if (((Integer)aTempData.get(12)).intValue() == 4) {
            week = "四";
          } else if (((Integer)aTempData.get(12)).intValue() == 5) {
            week = "五";
          } else if (((Integer)aTempData.get(12)).intValue() == 6) {
            week = "六";
          } else if (((Integer)aTempData.get(12)).intValue() == 7) {
            week = "日";
          } 
          if (((Integer)aTempData.get(12)).intValue() != nowWeek) {
            if (((Integer)aTempData.get(12)).intValue() == 1) {
              pc.sendPackets((ServerBasePacket)new S_ServerMessage(166, "使用时间限制:星期【一】"));
            } else if (((Integer)aTempData.get(12)).intValue() == 2) {
              pc.sendPackets((ServerBasePacket)new S_ServerMessage(166, "使用时间限制:星期【二】"));
            } else if (((Integer)aTempData.get(12)).intValue() == 3) {
              pc.sendPackets((ServerBasePacket)new S_ServerMessage(166, "使用时间限制:星期【三】"));
            } else if (((Integer)aTempData.get(12)).intValue() == 4) {
              pc.sendPackets((ServerBasePacket)new S_ServerMessage(166, "使用时间限制:星期【四】"));
            } else if (((Integer)aTempData.get(12)).intValue() == 5) {
              pc.sendPackets((ServerBasePacket)new S_ServerMessage(166, "使用时间限制:星期【五】"));
            } else if (((Integer)aTempData.get(12)).intValue() == 6) {
              pc.sendPackets((ServerBasePacket)new S_ServerMessage(166, "使用时间限制:星期【六】"));
            } else if (((Integer)aTempData.get(12)).intValue() == 7) {
              pc.sendPackets((ServerBasePacket)new S_ServerMessage(166, "使用时间限制:星期【日】"));
            } 
          } else if (((Integer)aTempData.get(12)).intValue() > 0 && (
            (Integer)aTempData.get(13)).intValue() > 0 && (
            (Integer)aTempData.get(14)).intValue() > 0) {
            if (((Integer)aTempData.get(12)).intValue() != nowWeek || 
              nowHour < ((Integer)aTempData.get(13)).intValue() || 
              nowHour >= ((Integer)aTempData.get(14)).intValue()) {
              pc.sendPackets((ServerBasePacket)new S_ServerMessage(166, 
                    "使用时间限制:星期【" + week + "】时间【" + ((Integer)aTempData.get(13)).intValue() + "】点，至【" + (
                    (Integer)aTempData.get(14)).intValue() + "】点"));
              return false;
            } 
          } else if (((Integer)aTempData.get(12)).intValue() < 0 && (
            (Integer)aTempData.get(13)).intValue() >= 0 && (
            (Integer)aTempData.get(14)).intValue() >= 0 && (
            nowHour < ((Integer)aTempData.get(13)).intValue() || 
            nowHour >= ((Integer)aTempData.get(14)).intValue())) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(166, "使用时间限制:时间【" + ((Integer)aTempData.get(13)).intValue() + 
                  "】点，至【" + ((Integer)aTempData.get(14)).intValue() + "】点"));
            return false;
          } 
          boolean isCreate = true;
          if (((Integer)aTempData.get(4)).intValue() != 0 && ((Integer)aTempData.get(5)).intValue() != 0) {
            int materials = ((Integer)aTempData.get(4)).intValue();
            int counts = ((Integer)aTempData.get(5)).intValue();
            if (!pc.getInventory().checkItem(materials, counts)) {
              L1Item temp = ItemTable.get().getTemplate(materials);
              pc.sendPackets((ServerBasePacket)new S_ServerMessage(337, String.valueOf(String.valueOf(temp.getName())) + "(" + (
                    counts - pc.getInventory().countItems(temp.getItemId())) + ")"));
              isCreate = false;
            } 
          } 
          if (isCreate) {
            pc.getInventory().consumeItem(((Integer)aTempData.get(4)).intValue(), (
                (Integer)aTempData.get(5)).intValue());
            if ((int[])aTempData.get(20) != null) {
              int[] Skills = (int[])aTempData.get(20);
              int time = ((Integer)aTempData.get(21)).intValue();
              int j = 0;
              while (j < Skills.length) {
                L1SkillUse l1skilluse = new L1SkillUse();
                l1skilluse.handleCommands(pc, Skills[j], pc.getId(), pc.getX(), pc.getY(), time, 4);
                pc.sendPacketsAll((ServerBasePacket)new S_DoActionGFX(pc.getId(), 19));
                j++;
              } 
            } 
            if (aTempData.get(11) != null)
              pc.sendPackets((ServerBasePacket)new S_SystemMessage("\\fY" + aTempData.get(11))); 
            if (((Integer)aTempData.get(9)).intValue() != 0) {
              L1Teleport.teleport(pc, ((Integer)aTempData.get(7)).intValue(), (
                  (Integer)aTempData.get(8)).intValue(), 
                  (short)((Integer)aTempData.get(9)).intValue(), 5, true);
              if (((Integer)aTempData.get(9)).intValue() != 0 && (
                (Integer)aTempData.get(10)).intValue() != 0) {
                pc.get_other().set_usemap(((Integer)aTempData.get(9)).intValue());
                ServerUseMapTimer.put(pc, ((Integer)aTempData.get(10)).intValue());
                pc.sendPackets(
                    (ServerBasePacket)new S_ServerMessage("时间限制:" + ((Integer)aTempData.get(10)).intValue() + "秒"));
              } 
            } 
          } 
        } else {
          if (pc.getInventory().checkItem(((Integer)aTempData.get(4)).intValue(), (
              (Integer)aTempData.get(5)).intValue()))
            pc.getInventory().consumeItem(((Integer)aTempData.get(4)).intValue(), (
                (Integer)aTempData.get(5)).intValue()); 
          pc.sendPackets((ServerBasePacket)new S_SystemMessage("\\fY" + aTempData.get(16)));
          if (((Integer)aTempData.get(19)).intValue() != 0)
            L1Teleport.teleport(pc, ((Integer)aTempData.get(17)).intValue(), (
                (Integer)aTempData.get(18)).intValue(), 
                (short)((Integer)aTempData.get(19)).intValue(), 5, true); 
        } 
      } 
      i++;
    } 
    return false;
  }
  
  private static boolean checkHasCastle(L1PcInstance player, int castle_id) {
    if (player.getClanid() != 0) {
      L1Clan clan = WorldClan.get().getClan(player.getClanname());
      if (clan != null && clan.getCastleId() == castle_id)
        return true; 
    } 
    return false;
  }
  
  private static void getData() {
    Connection con = null;
    try {
      con = DatabaseFactory.get().getConnection();
      Statement stat = con.createStatement();
      ResultSet rset = stat.executeQuery("SELECT * FROM w_城堡状态师");
      ArrayList<Object> aReturn = null;
      String sTemp = null;
      if (rset != null)
        while (rset.next()) {
          aReturn = new ArrayList();
          aReturn.add(0, new Integer(rset.getInt("npcid")));
          sTemp = rset.getString("action");
          aReturn.add(1, sTemp);
          aReturn.add(2, Integer.valueOf(rset.getInt("确认等级")));
          aReturn.add(3, Integer.valueOf(rset.getInt("确认职业")));
          aReturn.add(4, Integer.valueOf(rset.getInt("确认道具")));
          aReturn.add(5, Integer.valueOf(rset.getInt("确认道具数量")));
          aReturn.add(6, null);
          aReturn.add(7, new Integer(rset.getInt("座标X")));
          aReturn.add(8, new Integer(rset.getInt("座标Y")));
          aReturn.add(9, new Integer(rset.getInt("地图ID")));
          aReturn.add(10, new Integer(rset.getInt("进入时间")));
          aReturn.add(11, rset.getString("进入字语"));
          aReturn.add(12, new Integer(rset.getInt("限制星期")));
          aReturn.add(13, new Integer(rset.getInt("开始进入时间")));
          aReturn.add(14, new Integer(rset.getInt("结束进入时间")));
          aReturn.add(15, new Integer(rset.getInt("城堡编号")));
          aReturn.add(16, rset.getString("非法进入字语"));
          aReturn.add(17, new Integer(rset.getInt("非城堡传输座标X")));
          aReturn.add(18, new Integer(rset.getInt("非城堡传输座标Y")));
          aReturn.add(19, new Integer(rset.getInt("非城堡传输地图ID")));
          if (rset.getString("魔法Buff") != null && !rset.getString("魔法Buff").equals("") && 
            !rset.getString("魔法Buff").equals("0")) {
            aReturn.add(20, getArray(rset.getString("魔法Buff"), ",", 1));
          } else {
            aReturn.add(20, null);
          } 
          aReturn.add(21, new Integer(rset.getInt("魔法Buff时间")));
          aData.add(aReturn);
        }  
      if (con != null && !con.isClosed())
        con.close(); 
    } catch (Exception exception) {}
  }
  
  private static Object getArray(String s, String sToken, int iType) {
    StringTokenizer st = new StringTokenizer(s, sToken);
    int iSize = st.countTokens();
    String sTemp = null;
    if (iType == 1) {
      int[] iReturn = new int[iSize];
      int i = 0;
      while (i < iSize) {
        sTemp = st.nextToken();
        iReturn[i] = Integer.parseInt(sTemp);
        i++;
      } 
      return iReturn;
    } 
    if (iType == 2) {
      String[] sReturn = new String[iSize];
      int i = 0;
      while (i < iSize) {
        sTemp = st.nextToken();
        sReturn[i] = sTemp;
        i++;
      } 
      return sReturn;
    } 
    if (iType == 3) {
      String sReturn2 = null;
      int i = 0;
      while (i < iSize) {
        sTemp = sReturn2 = st.nextToken();
        i++;
      } 
      return sReturn2;
    } 
    return null;
  }
}
